package com.ocom.accesservice.controller;


import com.ocom.accesservice.service.AreaInfoService;
import com.ocom.accesservice.service.GroupPerService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.constant.UserConstants;
import com.ocom.common.dto.AreaDto;
import com.ocom.common.entity.access.EntityMjGroupPer;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.AreaInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class WxAreaController {

    @Autowired
    GroupPerService groupPerService;

    @Autowired
    AreaInfoService areaInfoService;

    @PostMapping(AccessServiceUrl.URL_WxGetAreaInfo)
    public Result getAreaList(Long companyId,Long pId){
//        List<AreaInfoVo> areaInfoVoList=new ArrayList<>();
//        //1.��ȡ�����б�
//        List<EntityMjGroupPer> groupPers=groupPerService.getList(companyId,pId);
//        if(groupPers!=null && groupPers.size()>0){
//            List<Long> areaIds=groupPers.stream().map(EntityMjGroupPer->EntityMjGroupPer.getAreaId()).collect(Collectors.toList());
        List<AreaInfoVo> areaInfoVoList = areaInfoService.getAreaList(companyId);
//        }
        return ResultUtil.success(areaInfoVoList);
    }


    @PostMapping(AccessServiceUrl.URL_WxGetAreaInfoById)
    public Result getAreaInfoById(Long companyId,Long areaNo){
        return ResultUtil.success(areaInfoService.getAreaInfoById(companyId,areaNo));
    }

    @PostMapping(AccessServiceUrl.URL_WxGetAccessByUser)
    public Result getAccessByUser(Long companyId,Long pId){
        return ResultUtil.success(groupPerService.getListByUid(companyId,pId));
    }

    @PostMapping(AccessServiceUrl.URL_WxNewArea)
    public Result newArea(Long companyId, @RequestParam(required = false) Long regionId, String areaName, Long pId){
        if ((UserConstants.NOT_UNIQUE.equals(areaInfoService.checkAreaNameUnique(companyId,areaName)))){
            return ResultUtil.error(ResultEnum.Err_3000);
        }
        return ResultUtil.success(areaInfoService.newArea(companyId,regionId,areaName,pId));
    }





}
